package com.stu.day11.clone;

/**
 * 副本中的引用类型字段仍然指向原始对象中的同一内存地址。
 * 因此，浅克隆的副本和原始对象会共享某些字段，特别是引用类型字段。
 * @author mm
 * @date 2025/1/16 11:34:50
 */
public class CloneTest {
    public static void main(String[] args) {
        Book book1 = new Book(1,"书1","作者甲");
        Book book2 = new Book(2,"书2","作者乙");
        Book[] books1 = new Book[]{book1};
        Book[] books2 = new Book[]{book1,book2};

        User user = new User(1,"张三",23,books1);
        System.out.println("原来的user:\t\t\t"+user);

        try {
            User usercopy = (User) user.clone();
            usercopy.setId(2);
            usercopy.setName("李四");
            usercopy.setAge(24);
//            usercopy.setBook(books2);
            usercopy.getBook()[0].setAuthor("王五");

            System.out.println("修改后的usercopy:\t"+usercopy);
            System.out.println("被浅拷贝后的user:\t"+user);


        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }

    }
}
